package wilfred.alpha;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class WilfredActivity extends Activity implements OnClickListener, OnCheckedChangeListener, SensorEventListener{

	SensorManager mSensorManager;

	Button mForward, mLeft, mRight, mReverse, mStartStop;
	TextView mXAccel, mYAccel, mZAccel, mDebug;
	ToggleButton mSendCommands;
	RadioGroup mCommandType; 

	float mX, mY, mZ;

	enum CommandType { DIRECT, ACCELEROMETER };
	enum DirectCommandDirection { NODIRECTION, FORWARD, LEFT, RIGHT, REVERSE };
	String[] direction = { "No Direction", "Forward", "Left", "Right", "Reverse" };

	boolean mIsSendCommand;
	CommandType mCommand;
	DirectCommandDirection mDirectDirection, mPrevDirectDirection;

	Thread mThread;
	final int SLEEP_TIME = 50;
	boolean mIsRunning;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		initialize();
		
	}

	private void initialize() {
		// Initialize Buttons
		mForward = (Button) findViewById(R.id.bForward);
		mLeft = (Button) findViewById(R.id.bLeft);
		mRight = (Button) findViewById(R.id.bRight);
		mReverse = (Button) findViewById(R.id.bReverse);
		mStartStop = (Button) findViewById(R.id.bStartStop);

		// Initialize Text Views
		mXAccel = (TextView) findViewById(R.id.tvXAccel);
		mYAccel = (TextView) findViewById(R.id.tvYAccel);
		mZAccel = (TextView) findViewById(R.id.tvZAccel);
		mDebug = (TextView) findViewById(R.id.tvDebug);
		

		// Initialize Toggle Buttons
		mSendCommands = (ToggleButton) findViewById(R.id.tbSendCommands);

		// Initialize Radio Groups
		mCommandType = (RadioGroup) findViewById(R.id.rgCommandType);

		// Set Listeners
		
		mForward.setOnClickListener(this);
		mLeft.setOnClickListener(this);
		mRight.setOnClickListener(this);
		mReverse.setOnClickListener(this);
		mStartStop.setOnClickListener(this);
		mSendCommands.setOnClickListener(this);
		mCommandType.setOnCheckedChangeListener(this);
		
	
		// State Variables
		mIsSendCommand = true;
		mCommand = CommandType.ACCELEROMETER;
		mDirectDirection = DirectCommandDirection.NODIRECTION;
		mPrevDirectDirection = DirectCommandDirection.NODIRECTION;

		
		// Accelerometer
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		if (mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
			Sensor s = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
			mSensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
		}
		
		mIsRunning = false;
	}


	public void onClick(View arg0) {
		switch(arg0.getId()) {
		case R.id.bForward:
			mPrevDirectDirection = mDirectDirection;
			mDirectDirection = DirectCommandDirection.FORWARD;
			break;

		case R.id.bLeft:
			mPrevDirectDirection = mDirectDirection;
			mDirectDirection = DirectCommandDirection.LEFT;
			break;

		case R.id.bRight:
			mPrevDirectDirection = mDirectDirection;
			mDirectDirection = DirectCommandDirection.RIGHT;
			break;

		case R.id.bReverse:
			mPrevDirectDirection = mDirectDirection;
			mDirectDirection = DirectCommandDirection.REVERSE;
			break;

		case R.id.bStartStop:
			if (mDirectDirection != DirectCommandDirection.NODIRECTION) {
				mPrevDirectDirection = mDirectDirection;
				mDirectDirection = DirectCommandDirection.NODIRECTION;
			} else {
				mDirectDirection = mPrevDirectDirection;
				mPrevDirectDirection = DirectCommandDirection.NODIRECTION;
			}
			break;

		case R.id.tbSendCommands:
			if (mSendCommands.isChecked()) 
				mIsSendCommand = true;
			else
				mIsSendCommand = false;
			break;
		}
		
		if (mCommand == CommandType.DIRECT)
			sendInfoToBluetooth();
	}


	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId) {
		case R.id.rbAccel:
			mCommand = CommandType.ACCELEROMETER;
			break;

		case R.id.rbDirect:
			mCommand = CommandType.DIRECT;
			break;
		}
	}


	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
	}

	public void onSensorChanged(SensorEvent event) {
		mX = event.values[0];
		mY = event.values[1];
		mZ = event.values[2];

		mXAccel.setText(getString(R.string.sXAccel) + " " + mX);
		mYAccel.setText(getString(R.string.sYAccel) + " " + mY);
		mZAccel.setText(getString(R.string.sZAccel) + " " + mZ);
		
		if (mCommand == CommandType.ACCELEROMETER)
			sendInfoToBluetooth();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mIsRunning = false;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mIsRunning = true;
	}
	
	public void sendInfoToBluetooth() {
		if (mIsRunning) {

			try {
				Thread.sleep(SLEEP_TIME);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}


			if (mIsSendCommand) {
				switch (mCommand) {
				case DIRECT:
					// TODO: Send Info to Bluetooth module
					mDebug.setText("DIRECT: " + mDirectDirection.name());
					break;

				case ACCELEROMETER:
					mDebug.setText("ACCELEROMETER: " + mX + "\t " + mY + "\t" + mZ);
					break;
				}
			}
		} 
	}
}